<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            overflow: hidden;
        }
    </style>
</head>

<body>
    <canvas id="canvas"></canvas>
</body>
<script id="ColorVS" type="x-shader/x-vertex">
    attribute vec4 a_Position;
    attribute vec2 a_Pin;
    uniform mat4 u_ModelM4;
    uniform mat4 u_PvM4;
    varying vec2 v_Pin;
    void main(){
        gl_Position = u_PvM4 * u_ModelM4 * a_Position;
        gl_PointSize = 30.0;
        v_Pin = a_Pin;
    }
</script>
<script id="ColorFS" type="x-shader/x-fragment">
    precision mediump float;
    uniform sampler2D u_Texture;
    uniform vec4 u_Color;
    varying vec2 v_Pin;
    void main(){
        gl_FragColor = texture2D(u_Texture, v_Pin);
        //gl_FragColor = u_Color;
    }
</script>
<script type="module">
    import { Matrix4, Vector3, Color, OrthographicCamera, PerspectiveCamera, Spherical } from '../js/three.module.js';
    import { CreateProgram, imgPromise } from '../js/Utils.js';
    import lib from '../js/ThreeLib.js';
    import CameraController from '../js/CameraController.js'
    import Poly from '../js/Poly4.js'
    import Control from '../js/Control.js'
    import Earth from '../js/objs/Earth.js'
    import Geography from '../js/Geography.js'

    import Geo from '../js/Poly2/Geo.js'
    import Mat from '../js/Poly2/Mat.js'
    import Obj3D from '../js/Poly2/Obj3D.js'
    import Scene from '../js/Poly2/Scene.js'





    //#region gl
    const canvas = document.getElementById("canvas");
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;

    const gl = canvas.getContext("webgl", { antialias: true, });

    gl.enable(gl.BLEND);
    gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
    gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);//去图片描边
    gl.enable(gl.DEPTH_TEST);
    //#endregion

    const scene = new Scene({ gl: gl });
    scene.addAxisProgram();
    scene.regProgram('color', {
        program: CreateProgram(gl,
            document.querySelector('#ColorVS').innerText,
            document.querySelector('#ColorFS').innerText),
        attribs: ['a_Position', 'a_Pin'],
        uniforms: ['u_PvM4', 'u_ModelM4', 'u_Texture', 'u_Color']
    })


    const pvM4 = new Matrix4();
    const modelM4 = new Matrix4();
    const eye = new Vector3(0, 0, 2);
    const lookAt = new Vector3();

    const camera = new PerspectiveCamera(45, canvas.width / canvas.height, 1, 20);
    const cameraController = new CameraController({
        canvas,
        camera,
        pvMatrix: pvM4,
        eye,
        lookAt,
    });



    const axis = new Obj3D().createAxis('axis');
    scene.add(axis);




    const earth = new Earth(0.5, 64, 32);
    const [earthVertices, earthElements, earthNormals, earthUv] = [
        earth.vertices, earth.elements, earth.normals, earth.uv
    ];

    const earthGeo = new Geo({
        data: {
            'a_Position': { size: 3, array: new Float32Array(earthVertices) },
            'a_Pin': { size: 2, array: new Float32Array(earthUv) },
        },
        element: { array: new Uint16Array(earthElements) }
    });
    const earthMat = new Mat({
        program: 'color',
        mode: ['TRIANGLES'],
        data: {
            'u_PvM4': { type: 'uniformMatrix4fv', value: pvM4.elements },
            'u_ModelM4': { type: 'uniformMatrix4fv', value: modelM4.elements },
            'u_Color': { type: 'uniform4fv', value: [1, 1, 1, 1] },
        }
    });


    const earthImage = new Image();
    earthImage.src = '../Image/earth.jpg';
    earthImage.addEventListener('load', function () {
        earthMat.maps = { 'u_Texture': { image: earthImage } };

        const earth3D = new Obj3D({ geo: earthGeo, mat: earthMat });
        scene.add(earth3D);
    });

    (function ani() {
        draw();
        requestAnimationFrame(ani)
    })();


    function draw() {
        scene.setUniform('u_PvM4', { value: pvM4.elements });
        scene.draw();
    }
</script>

</html>